package org.example.wx_springboot.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.wx_springboot.common.Result;
import org.example.wx_springboot.controller.request.CommodityPageRequest;
import org.example.wx_springboot.entity.Commodity;
import org.example.wx_springboot.service.ICommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin//这个注解是解决跨域错误的
@RestController
@Slf4j
@RequestMapping("/commodity")
public class CommodityController {

    @Autowired
    ICommodityService commodityservice;


    @GetMapping("/commod")
    public Result list(){
        List<Commodity> commodity = commodityservice.list();
        return Result.success(commodity);//返回CommodityMapper的数据
    }


//    后台添加列表
    @PostMapping("/seve")
    public Result save(@RequestBody Commodity commodity){
        commodityservice.save(commodity);
        return Result.success();
    }


//    模糊查询
    @GetMapping("/page")
    //    将查询类放进来，放进来后去CommodityService将name和phone拿出来
    public Result page(CommodityPageRequest commodityPageRequest) {
//        配置完去ICommodityService实践一下
        return Result.success(commodityservice.page(commodityPageRequest));
    }


//    提供一个id查询接口
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Commodity commodity = commodityservice.getById(id);
        return Result.success(commodity);
    }






}
